<?php

namespace app\logic\push;

use app\model\MessageSystem AS ModelMessageSystem;
use app\model\MessageAdmin AS ModelMessageAdmin;
use app\model\MessageClient AS ModelMessageClient;
use app\model\AdminInfo AS ModelAdminInfo;
use app\model\MessageContent as ModelMessageContent;

class AdminPush extends Push
{
    /**
     * 发布系统消息
     * @param string $str_client 客户端名称 多个用,隔开
     * @param string $str_title 标题
     * @param string $str_desc 推送标题(描述)
     * @param int $int_release_status 发布状态 0-暂不发布 1-立即发布 2-预约发布
     * @param string $str_subscribe_time 预约时间
     * @param int $int_is_push 是否已经推送 0-未推送 1-已推送
     * @param string $str_push_type 推送类型 text-图文 url-地址跳转
     * @param string $str_content 内容(push_type值对应该字段的值)
     * @param string $str_url 跳转地址
     * @param string $str_remark 备注
     * @param int $int_admin_id 管理员编号
     */
    public function releaseSystemMessage($str_client, $str_title, $str_desc, $int_release_status, $str_subscribe_time, $int_is_push, $str_push_type, $str_content, $str_url, $str_remark, $int_admin_id)
    {
        _toCompare(empty($str_title), '===', true, 10101); // 标题不能为空

        _toCompare(empty($str_desc), '===', true, 10102); // 推送标题不能为空

        _toCompare($int_release_status, '==', '', 10103); // 请选择发布状态

        _toCompare(in_array($int_release_status, [0, 1, 2]), '===', false, 10103); // 请选择发布状态

        if ($int_release_status == 2) {
            _toValidate('time', $str_subscribe_time, 10104); // 请选择预约时间
            _toCompare(date('Y-m-d H:i:s'), '>', $str_subscribe_time, 10105); // 预约时间必须大于当前时间
        } else {
            $str_subscribe_time = '0000-00-00 00:00:00';
        }

        _toCompare(in_array($int_is_push, [0, 1]), '===', false, 10113); // 请选择是否立即推送

        _toCompare(empty($str_client), '===', true, 10106); // 请选择发送的客户端

        if ($str_client == 'all') {
            $str_client = implode(',', $this->arr_client_key);
        }

        _toCompare(in_array($str_push_type, ['text', 'url']), '===', false, 10107); // 请选择推送类型

        if ($str_push_type == 'text') {
            _toCompare(empty($str_content), '===', true, 10108); // 请输入推送内容
            $str_message_content = $str_content;
            $str_message_id = ModelMessageContent::getInstance()->toAdd([
                'title' => $str_title,
                'desc' => $str_desc,
                'content' => $str_message_content,
            ]);
            $str_massage_url = '';
            $int_type = 1;
        } else {
            _toCompare(empty($str_url), '===', true, 10109); // 请输入url地址
            $str_message_id = '';
            $str_message_content = '';
            $str_massage_url = $str_url;
            $int_type = 2;
            $str_content = $str_url;
        }

        $json_params = $this->rules($int_type, $str_message_id, $str_massage_url, '', $str_message_content, []);

        $arr_client = explode(',', $str_client);
        $arr_new_client = [];
        foreach ($arr_client as $value) {
            if (in_array($value, $this->arr_client_key)) {
                if ($int_is_push == 1) {
                    $obj = new PushMessage(config('mipush.' . $value));
                    $obj->sendToAll('all', $str_title, $str_desc, $json_params, 0);
                }
                $arr_new_client[] = $value;
            }
        }

        if ($int_is_push == 1) {
            $str_release_time = date('Y-m-d H:i:s');
            $str_push_time = date('Y-m-d H:i:s');
        } else {
            $str_release_time = '0000-00-00 00:00:00';
            $str_push_time = '0000-00-00 00:00:00';
        }

        _toCompare($arr_new_client, '==', [], 10110); // 消息推送失败请重试

        $arr_data = [
            'client' => implode(',', $arr_new_client),
            'title' => $str_title,
            'desc' => $str_desc,
            'release_status' => $int_release_status,
            'release_time' => $str_release_time,
            'subscribe_time' => $str_subscribe_time,
            'push_time' => $str_push_time,
            'is_push' => $int_is_push,
            'params' => $json_params,
            'push_type' => $str_push_type,
            'content' => $str_content,
            'remark' => $str_remark,
            'admin_id' => $int_admin_id,
        ];

        ModelMessageSystem::getInstance()->toAdd($arr_data);

        _returnData();
    }

    /**
     * 获取所有客户端
     */
    public function getAllClient()
    {
        _returnData($this->arr_client);
    }

    /**
     * 消息推送（多人）（发布活动消息）
     * @param string $str_map_id 用户编号 多个用,隔开
     * @param string $str_client 客户端名称
     * @param string $str_title 标题
     * @param string $str_desc 推送标题(描述)
     * @param int $int_release_status 发布状态 0-暂不发布 1-立即发布 2-预约发布
     * @param string $str_subscribe_time 预约时间
     * @param int $int_is_push 是否已经推送 0-未推送 1-已推送
     * @param string $str_push_type 推送类型 text-图文 url-地址跳转
     * @param string $str_content 内容(push_type值对应该字段的值)
     * @param string $str_url 跳转地址
     * @param string $str_platform 平台名称 all-所有 android-安卓 ios-苹果
     * @param int $int_admin_id 管理员编号
     */
    public function releaseActivityMessage($str_map_id, $str_client, $str_title, $str_desc, $int_release_status, $str_subscribe_time, $int_is_push, $str_push_type, $str_content, $str_url, $str_platform, $int_admin_id)
    {
        _toCompare(empty($str_title), '===', true, 10101); // 标题不能为空

        _toCompare(empty($str_desc), '===', true, 10102); // 推送标题不能为空

        _toCompare($int_release_status, '==', '', 10103); // 请选择发布状态
        _toCompare(in_array($int_release_status, [1, 2]), '===', false, 10103); // 请选择发布状态

        if ($int_release_status == 2) {
            _toValidate('time', $str_subscribe_time, 10104); // 请选择预约时间
            _toCompare(date('Y-m-d H:i:s'), '>', $str_subscribe_time, 10105); // 预约时间必须大于当前时间

            $int_is_show = 2;
        } else {
            $str_subscribe_time = '0000-00-00 00:00:00';
            $int_is_show = 1;
        }

        _toCompare(in_array($int_is_push, [0, 1]), '===', false, 10113); // 请选择是否立即推送

        _toCompare(in_array($str_client, $this->arr_client_key), '===', false, 10106); // 请选择发送的客户端

        _toCompare(in_array($str_push_type, ['text', 'url']), '===', false, 10107); // 请选择推送类型

        if ($str_push_type == 'text') {
            _toCompare(empty($str_content), '===', true, 10108); // 请输入推送内容
            $str_massage_url = '';
            $str_message_content = $str_content;
            $str_message_id = ModelMessageContent::getInstance()->toAdd([
                'title' => $str_title,
                'desc' => $str_desc,
                'content' => $str_message_content,
            ]);
            $int_type = 3;
        } else {
            _toCompare(empty($str_url), '===', true, 10109); // 请输入url地址
            $str_message_id = '';
            $str_massage_url = $str_url;
            $int_type = 2;
            $str_message_content = '';
        }

        _toCompare(in_array($str_platform, ['android', 'ios', 'all']), '===', false, 10112); // 请选择需要发送的平台

        $json_params = $this->rules($int_type, $str_message_id, $str_massage_url, '', $str_message_content, []);

        //_toCompare(empty($arr_passport), '===', true, 10111); // 请选择需进行推送的用户

        if ($int_is_push == 1) {
            $str_release_time = date('Y-m-d H:i:s');
            $str_push_time = date('Y-m-d H:i:s');

        } else {
            $str_release_time = '0000-00-00 00:00:00';
            $str_push_time = '0000-00-00 00:00:00';
        }

        $arr_data = [
            'client' => $str_client,
            'title' => $str_title,
            'desc' => $str_desc,
            'release_status' => $int_release_status,
            'release_time' => $str_release_time,
            'subscribe_time' => $str_subscribe_time,
            'push_time' => $str_push_time,
            'is_push' => $int_is_push,
            'params' => $json_params,
            'push_type' => $str_push_type,
            'content' => $str_content,
            'platform' => $str_platform,
            'admin_id' => $int_admin_id,
        ];

        $int_message_id = ModelMessageAdmin::getInstance()->toAdd($arr_data);

        $this->pushMessageToUser($str_client, $str_map_id, $str_platform, $int_is_push, $str_title, $str_desc, $json_params, $int_is_show, $int_release_status, $str_subscribe_time, 0, $int_message_id);

        _returnData();
    }

    /**
     * 获取系统消息列表
     * @param int $int_now_page 当前页码
     * @param int $int_list_rows 一页条数
     * @param int $int_admin_id 管理员编号
     */
    public function getSystemMessageList($int_now_page, $int_list_rows)
    {
        $obj = ModelMessageSystem::getInstance();
        $arr_where = [];
        $str_field = 'id AS messageId,title,admin_id,push_type AS pushType,release_time AS releaseTime,release_status AS releaseStatus,is_push AS isPush,push_time AS pushTime,client';
        $arr_info = \app\logic\Page::getInstance()->getList($obj, $arr_where, $str_field, $int_now_page, $int_list_rows, 'gmt_create DESC', 'message');
        if (isset($arr_info['messageList']) && empty($arr_info['messageList']) === false) {
            foreach ($arr_info['messageList'] as &$value) {
                $value['adminName'] = (string)ModelAdminInfo::getInstance()->toValue(['id' => $value['admin_id']], 'admin_name');
                unset($value['admin_id']);
            }
        }
        _returnData($arr_info);
    }

    /**
     * 获取活动消息列表
     * @param int $int_now_page 当前页码
     * @param int $int_list_rows 一页条数
     * @param array $arr_where 查询条件
     */
    public function getActivityMessageList($int_now_page, $int_list_rows, $arr_where = [])
    {
        $obj = ModelMessageAdmin::getInstance();
        $str_field = 'id AS messageId,title,admin_id,push_type AS pushType,release_time AS releaseTime,release_status AS releaseStatus,is_push AS isPush,push_time AS pushTime,client';
        $arr_info = \app\logic\Page::getInstance()->getList($obj, $arr_where, $str_field, $int_now_page, $int_list_rows, 'gmt_create DESC', 'message');
        if (isset($arr_info['messageList']) && empty($arr_info['messageList']) === false) {
            foreach ($arr_info['messageList'] as &$value) {
                $value['adminName'] = (string)ModelAdminInfo::getInstance()->toValue(['id' => $value['admin_id']], 'admin_name');
                unset($value['admin_id']);
            }
        }
        _returnData($arr_info);
    }

    /**
     * 获取用户消息列表
     * URI:/admin/get.user.message.list
     * @param int $int_now_page 当前页码
     * @param int $int_list_rows 一页条数
     * @param $int_user_id
     */
    public function getUserMessageList($int_now_page, $int_list_rows, $int_user_id)
    {
        $obj = ModelMessageClient::getInstance();
        $arr_where = [
            'client' => 'user',
            'map_id' => $int_user_id,
            'is_show' => 1,
        ];
        $str_field = 'id AS messageId,title,desc,gmt_create AS gmtCreate';
        $arr_info = \app\logic\Page::getInstance()->getList($obj, $arr_where, $str_field, $int_now_page, $int_list_rows, 'gmt_create DESC', 'message');
        _returnData($arr_info);
    }

    /**
     * 获取用户消息详情
     * @param $int_message_id
     * @param $int_user_id
     */
    public function getUserMessageInfo($int_message_id, $int_user_id)
    {
        $obj = ModelMessageClient::getInstance();
        $arr_where = [
            'id' => $int_message_id,
            'map_id' => $int_user_id,
            'is_show' => 1,
        ];
        $str_field = 'id AS messageId,title,desc,params,gmt_create AS gmtCreate';
        $arr_info = (array)$obj->toFind($arr_where, $str_field);
        _returnData($arr_info);
    }


    /**
     * 获取系统消息详情
     * @param $int_message_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSystemMessageInfo($int_message_id)
    {
        $obj = ModelMessageSystem::getInstance();
        $arr_where = [
            'id' => $int_message_id,
        ];
        $str_field = 'id AS messageId,title,desc,admin_id,push_type AS pushType,release_time AS releaseTime,release_status AS releaseStatus,is_push AS isPush,push_time AS pushTime,client,content,params';
        $arr_info = (array)$obj->toFind($arr_where, $str_field);
        if (isset($arr_info['admin_id'])) {
            $arr_info['adminName'] = (string)ModelAdminInfo::getInstance()->toValue(['id' => $arr_info['admin_id']], 'admin_name');
            unset($arr_info['admin_id']);
        }

        _returnData($arr_info);
    }

    /**
     * 获取活动消息详情
     * @param $int_message_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getActivityMessageInfo($int_message_id)
    {
        $obj = ModelMessageAdmin::getInstance();
        $arr_where = [
            'id' => $int_message_id,
        ];
        $str_field = 'id AS messageId,title,desc,admin_id,push_type AS pushType,release_time AS releaseTime,release_status AS releaseStatus,is_push AS isPush,push_time AS pushTime,client,content,params';
        $arr_info = (array)$obj->toFind($arr_where, $str_field);
        if (isset($arr_info['admin_id'])) {
            $arr_info['adminName'] = (string)ModelAdminInfo::getInstance()->toValue(['id' => $arr_info['admin_id']], 'admin_name');
            unset($arr_info['admin_id']);
        }

        _returnData($arr_info);
    }

    /**
     * 发布系统消息
     */
    public function releaseSystemMessageInfo()
    {

    }

    /**
     * 立即推送系统消息
     */
    public function pushSystemMessageInfo()
    {

    }

    /**
     * 撤回系统消息
     */
    public function resetSystemMessageInfo()
    {

    }
}
